<html>
<head>

<title>Groovy Goodness: Using Groovy on the Command-Line</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Using Groovy on the Command-Line</h3>

<div class="post">
<p>We can use Groovy on the command-line to evaluate one-line scripts and pipe output to other commands or use input from other commands. This makes for some command-line scripting. We invoke groovy with the <code>-e</code> argument to <em>evaluate</em> a simple script. We can even use arguments in the script.</p><p>Other arguments are <code>-n</code> and <code>-p</code> to process each line of input from a file or from another command. We can also iterate over a list of files, change the contents and use the <code>-i</code> argument to define a backup file pattern when saving the files.</p><p>The last argument is <code>-l</code> to start Groovy in listening mode. We can use for example a telnet client to connect to groovy and do some processing.</p>
<pre class="brush:plain">
// Pipe groovy output to grep.
$ groovy -e "Locale.availableLocales.each { println it.displayName }" | grep English

// Or do it all in Groovy.
$ groovy -e "Locale.availableLocales.displayName.findAll { it =~ args[0] }.each { println it }" English

// Using -n and -p to filter each line.
$ groovy -e "Locale.availableLocales.each { println it.displayName }" | groovy -ne "if (line =~ 'English') println line"
$ groovy -e "Locale.availableLocales.each { println it.displayName }" | groovy -pe "if (line =~ 'English') line"

// All will output:
English
English (United States)
English (Malta)
English (United Kingdom)
English (New Zealand)
English (Philippines)
English (South Africa)
English (Ireland)
English (India)
English (Australia)
English (Canada)
English (Singapore)
</pre>
<p>Starting Groovy in listening mode:</p>
<pre class="brush:bash">
$ groovy -l 9000 -e "println 'You say: ' + line"
groovy is listening on port 9000

$ telnet localhost 9000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Groovy Rocks!
You say: Groovy Rocks!
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>